盒子
盒子
文章目录
  1. git config
  2. git add files to last commit
  3. git commit
    1. commit messages
    2. reset head to some commit
  4. setup difftool and merge tool
  5. delete a branch
  6. checkout using remote branch
  7. rebase local branch with remote branch
  8. git stash
  9. git rebase
  10. merge 的几种操作
  11. conflicts
  12. push some commit
  13. break a previous commit to multiple commit
  14. some tips
  15. git log
    1. show name recent changed
  16. git revert
  17. git clean untracked file
  18. git reflog
  19. change default editor to visual studio code

git 基本操作

git config

git 的自动换行会有些问题,有的时候会出现fatal error,故可以配置为自动转化为lf,或者关闭这个功能。

1
2
git config --global core.autocrlf input #提交到git是自动将换行符转换为lf
git config --global core.editor "vim" # use vim editor

git add files to last commit

git commit –amend –no-edit

git push -f origin some_branch

git clean -f

git clean -d

git prune

git cherry-pick

git merge --no-ff

git add -p # patch mode可以将一个文件分成多份来进行提交,适合于当一次内容里面修改了多方面的东西的时候。

git commit

commit messages

第一行为summary,少于50个字符,然后空一行,下面是正文,正文一行也要控制在72个字符。

reset head to some commit

setup difftool and merge tool

1
2
git config --global difftool.beyondcompare.cmd "\"c:\program files\beyond compare 4\bcomp.exe\" \"$LOCAL\" \"$REMOTE\""
git config --global diff.tool beyondcompare
1
2
git config --global merge.tool p4merge
git config --global mergetool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"

delete a branch

1
2
git push -d origin branchname
git branch -d branchname

checkout using remote branch

首先git fetch 获取到所有的branch,然后执行
git checkout -t <name of remote>/test or git checkout -b test <name of remote>/test

rebase local branch with remote branch

git pull –rebase origin master

git stash

1
2
3
git stash save XXX
git stash list
git stash apply

git rebase

rebase有两个方面,一个是个人部分,需要利用git rebase -i对已有的commit进行整理,另一方面是对于branch层面上的,避免merge的时候出现分叉,使用git pull --rebase来进行merge,同时也可以有两个设置

1
2
git config branch.dev.rebase true
git config --global branch.autosetuprebase always

merge 的几种操作

  • rebase
  • squash merge

rebase

squash

conflicts

the meaning of theirs and ours are not the same in git merge and git rebase, reference.

1
2
3
git checkout --theirs -- .
git checkout --ours . # checkout our local version of all files
git add -u # mark all conflicted files as merged

push some commit

Cherry-pick works best compared to all other methods while pushing a specific commit.
The way to do that is:

Create a new branch:

1
git branch <new-branch>

Update your new-branch with your origin branch:

1
2
git fetch
git rebase

These actions will make sure that you exactly have the same stuff as your origin has.
Cherry-pick the sha id that you want to do push:

1
git cherry-pick <sha id of the commit>

Push it to your origin:

1
git push

Run gitk to see that everything looks the same way you wanted.

break a previous commit to multiple commit

git rebase -i will do it.

First, start with a clean working directory: git status should show no pending modifications, deletions, or additions.

To split apart your most recent commit, first:

$ git reset HEAD~

Now commit the pieces individually in the usual way, producing as many commits as you need.

If it was farther back in the tree, then

$ git rebase -i HEAD~3

where 3 is how many commits back it is.

If it was farther back in the tree than you want to count, then

$ git rebase -i 123abcd~

where 123abcd is the SHA1 of the commit you want to split up.

When you get the rebase edit screen, find the commit you want to break apart. At the beginning of that line, replace pick with edit (e for short). Save the buffer and exit. Rebase will now stop just after the commit you want to edit. Then:

$ git reset HEAD~

Commit the pieces individually in the usual way, producing as many commits as you need, then

$ git rebase --continue

some tips

git push 之前记得做一次 git pull --rebase origin master.

git log

show name recent changed

git log –name-status -10 path/to/dir

git revert

git revert commit-id, add a new commit to revert that commit

git checkout <commit_hash> -- <file>

git clean untracked file

git clean -fd

git reflog

git reflog to get records before index.

change default editor to visual studio code

git config --global core.editor "code --wait"

呼呼呼山
2019-03-26 09:36:55